package com.ascainiao.control
{
	import com.ascainiao.AssetItem;
	import com.ascainiao.AssetType;
	import com.ascainiao.ProjectConfig;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.text.Font;
	import flash.utils.getDefinitionByName;

	/**
	 * @author 小飞侠
	 * @E-mail: 405076856@qq.com
	 * 创建时间：2021-12-8 下午5:42:41
	 *
	 */
	public class PreloadControl
	{
		//项目配置
		private var _config:ProjectConfig;
		//字体资源加载器
		private var font_loader:Loader=new Loader();
		//当前加载任务索引
		private var index:int=0;
		private var assets:Vector.<AssetItem>;
		public function PreloadControl(config:ProjectConfig)
		{
			_config=config;
			assets=config.assets;
			font_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onCompleteHandler);
			//开始加载
			startLoad();
		}
		private function startLoad():void{
			var src:String=assets[index].url;
			var type:int=assets[index].type;
			if(type==AssetType.FONT){
				font_loader.load(new URLRequest(_config.baseUrl+src),new LoaderContext(false,ApplicationDomain.currentDomain));
			}
		}
		private function onCompleteHandler(event:Event):void{
			var type:int=assets[index].type;
			if(type==AssetType.FONT){
				var MyFont:Class = getDefinitionByName("PingFangFont") as Class;
					Font.registerFont(MyFont);
					var myFont:Font = new MyFont() as Font;
			}
		}
	}
}